package com.yang.yangCommand;

import com.yang.YangMonster;
import com.yang.entity.Boss;
import com.yang.loader.DataLoader;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarFlag;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;

import java.util.ArrayList;
import java.util.List;

public class BossController implements Listener {
    // 获取到YangMonster配置文件
    private static YangMonster yangMonster;

    public BossController(YangMonster yangMonster) {
        this.yangMonster = yangMonster;
        bossYml = DataLoader.getData();
        loadBoss();
        startCheckTask();
    }

    private static YamlConfiguration bossYml;
    // 创建BossList集合用来存储所有Boss
    private static List<Boss> bossList = new ArrayList<>();
    //    private BossBar bossBar = null;

    BossBar bossBar = null;
    // 渲染boss.yml
    public void loadBoss() {
        //
//        BossBar bossBar = Bukkit.createBossBar("BOSS测试", BarColor.RED, BarStyle.SOLID);
        bossBar = Bukkit.createBossBar("BOSS测试", BarColor.RED, BarStyle.SOLID);
        bossBar.setProgress(1.0);
        bossBar.addFlag(BarFlag.DARKEN_SKY);
        bossBar.addFlag(BarFlag.PLAY_BOSS_MUSIC);
        bossBar.addFlag(BarFlag.CREATE_FOG);
        bossBar.setStyle(BarStyle.SEGMENTED_20);
        // 获取所有在线的玩家
        for (Player player : Bukkit.getOnlinePlayers()) {
            bossBar.addPlayer(player);
        }
        // 获取一个世界和位置
        World world = Bukkit.getWorld("world");
        Location location = new Location(world, 100, 71, 100);
        // 生成 Boss
//        Boss boss = (Boss) world.spawnEntity(location, EntityType.WITHER);
//        LivingEntity entity = (LivingEntity) world.spawnEntity(location, EntityType.GHAST);
        LivingEntity entity = (LivingEntity) world.spawnEntity(location, EntityType.ZOMBIE);
        entity.setMaxHealth(100);
        entity.setHealth(100);
        entity.addScoreboardTag("凋零恶魂");
        // 设置名称
        if (entity instanceof Boss) {
            bossBar = (BossBar) entity;
            // 设置名称

            // 设置其他 Boss 属性...

            // 将 Boss 添加到所有在线玩家
            for (Player player : Bukkit.getOnlinePlayers()) {
                // 添加目标玩家
                bossBar.addPlayer(player);
            }
        } else {
            // 如果生成的实体不是 Boss 类型，可以进行适当的处理
            yangMonster.getLogger().warning("Failed to create Boss entity.");
        }
    }

    @EventHandler
    public void onEntityDamage(EntityDamageEvent event) {
        Entity entity = event.getEntity();
        // 检查受到伤害的实体是否是存储的怪物实体
        if (entity instanceof LivingEntity && entity.getScoreboardTags().contains("凋零恶魂")) {
            yangMonster.log(ChatColor.RED, "受到伤害的实体是存储的怪物实体");
            LivingEntity monster = (LivingEntity) entity;
//            BossBar bossBar = bossBars.get(monster);

            // 更新 BossBar 进度，这里假设进度与怪物血量成正比
            double healthPercent = 0.0;
            if (monster.getHealth() > 0.00) {
                yangMonster.log(ChatColor.RED, String.valueOf(monster.getHealth()));
                yangMonster.log(ChatColor.RED, String.valueOf(monster.getMaxHealth()));
                healthPercent = monster.getHealth();
            }else {
                for (Player player : Bukkit.getOnlinePlayers()) {
                    // 添加目标玩家
                    bossBar.removePlayer(player);
                }
            }
            bossBar.setProgress(healthPercent);
        }
    }

    public void startCheckTask() {
//        for (Boss boss : bossList) {
//            final Boss currentBoss = boss; // 创建 final 副本
//            BukkitRunnable task = new BukkitRunnable() {
//                @Override
//                public void run() {
//                    // 每隔一定时间执行检查玩家位置的任务
//                    saveBoss(currentBoss);
//                }
//            };
//            task.runTaskTimer(yangMonster, 0, currentBoss.getTime() * 20);
//        }
        // 将 Boss Bar 添加到所有在线玩家
//        for (Player player : Bukkit.getOnlinePlayers()) {
//            bossList.get(0).addPlayer(player);
//            bossBar.addPlayer(player);
//        }

        // 创建并运行一个任务，用于更新 Boss Bar
//        Bukkit.getScheduler().runTaskTimer(this, () -> {
        // 更新 Boss Bar 的进度
//            bossBar.setProgress(Math.random());

        // 更新 Boss Bar 的标题
//            bossBar.setTitle("My Boss - " + System.currentTimeMillis());
//        }, 20L, 20L);
    }

    //根据参数生成怪物
    public void saveBoss(Boss boss) {
        //生成怪物
        World world = yangMonster.getServer().getWorld(boss.getWorld());
        EntityType entityType = EntityType.fromName(boss.getType());
        Location location = new Location(world, boss.getX(), boss.getY(), boss.getZ());
        Entity entity = world.spawnEntity(location, entityType);
        //添加标签
//        entity.addScoreboardTag(boss.getTag());
        //血量
//        entity.set(monster.getHp());
//        entity.setHealth(monster.getHp());
//        entity.setAI(true);
//        entity.setCustomNameVisible(true);
//        entity.setCustomName(monster.getName());
        yangMonster.log(ChatColor.RED, String.valueOf(entity));
    }

}
